﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using SportsStore.Builder;
using SportsStore.Model;
using SportsStore.Repository.Repositories;
using System.Collections.Generic;
using System.Configuration;
using System.Transactions;
using NUnit.Framework;

namespace SportsStore.IntegrationTests.Repositories
{
  [TestFixture]
  public class CartItemRepositoryTest
  {
    #region Setup/Teardown

    [SetUp]
    public void Setup()
    {
      _transactionScope = new TransactionScope();

      _product1 = BuildMeA.Product("Product1", "Description1", 10M);
      _product2 = BuildMeA.Product("Product2", "Description2", 50M);
      _product3 = BuildMeA.Product("Product3", "Description3", 100M);

      _cartItem1 = BuildMeA.CartItem(_product1.Id, "Key1", 1)
            .WithProduct(_product1);

      _cartItem2 = BuildMeA.CartItem(_product2.Id, "Key1", 5)
            .WithProduct(_product2);

      _cartItem3 = BuildMeA.CartItem(_product3.Id, "Key2", 1)
            .WithProduct(_product3);

      _cartItemRepository = new CartItemRepository(ConfigurationManager.ConnectionStrings["SportsStore"].ConnectionString);

      _cartItemRepository.Create(_cartItem1);
      _cartItemRepository.Create(_cartItem2);
      _cartItemRepository.Create(_cartItem3);
    }

    [TearDown]
    public void TearDown()
    {
      _transactionScope.Dispose();
    }

    #endregion

    private ProductRepository _productRepository;
    private CartItemRepository _cartItemRepository;
    private TransactionScope _transactionScope;
    private CartItem _cartItem1;
    private CartItem _cartItem2;
    private CartItem _cartItem3;
    private Product _product1;
    private Product _product2;
    private Product _product3;
    private const string _shoppingCardId = "Key1";

    [Test]
    public void GivenCartItems_WhenIAskForCartItemsForAProductId_IGetTheCartItemsForThatProduct()
    {
      CartItem cartItem = _cartItemRepository.GetCart(_product1.Id, _shoppingCardId);
      Assert.That(cartItem.CartId, Is.EqualTo(_shoppingCardId));
    }

    [Test]
    public void GivenCartItems_WhenIAskForCartItemsForAShoppingCartId_IGetTheCartItemsForThatShoppingCart()
    {
      IEnumerable<CartItem> cartItems = _cartItemRepository.GetCart(_shoppingCardId);
      Assert.That(cartItems.Count(), Is.EqualTo(2));
    }

    [Test]
    public void GivenCartItems_WhenIAskForTheTotalCostForAShoppingCart_IGetTheTotalAmount()
    {
      decimal TotalCost = _cartItemRepository.GetTotal(_shoppingCardId);
      Assert.That(TotalCost, Is.EqualTo(260M));
    }
  }
}
